﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKPatternSign.StairsRoomPlanSign.ModelLayer
{
	public class EvacuationRadiusInformationBean : IDimensionMake
	{
		public EvacuationRadiusInformationBean(List<Curve> lstInputCurve)
		{
			this.m_lstAllCurves = lstInputCurve;
		}

		public bool TryMakeSign(Autodesk.Revit.DB.Document inputDoc, ViewPlan useView)
		{
			bool result;
			try
			{
				GraphicsStyle wantStyle = this.GetWantStyle(inputDoc);
				LinePatternElement wantLinePattern = this.GetWantLinePattern(inputDoc);
				foreach (Curve curve in this.m_lstAllCurves)
				{
					DetailCurve detailCurve = inputDoc.Create.NewDetailCurve(useView, curve);
					if (wantStyle != null)
					{
						detailCurve.get_Parameter(this.m_useLineStyelParameter).Set(wantStyle.Id);
					}
					if (wantLinePattern != null)
					{
						OverrideGraphicSettings overrideGraphicSettings = new OverrideGraphicSettings();
						overrideGraphicSettings.SetProjectionLinePatternId(wantLinePattern.Id);
						inputDoc.ActiveView.SetElementOverrides(detailCurve.Id, overrideGraphicSettings);
					}
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private LinePatternElement GetWantLinePattern(Autodesk.Revit.DB.Document inputDoc)
		{
			foreach (Element element in new FilteredElementCollector(inputDoc).OfClass(typeof(LinePatternElement)).ToElements())
			{
				LinePatternElement linePatternElement = element as LinePatternElement;
				if (linePatternElement.Name.Equals(this.m_useLinePatternName))
				{
					return linePatternElement;
				}
			}
			return null;
		}

		private GraphicsStyle GetWantStyle(Autodesk.Revit.DB.Document inputDoc)
		{
			GraphicsStyle graphicsStyle = this.FindSuitStyle(inputDoc);
			if (graphicsStyle != null)
			{
				return graphicsStyle;
			}
			return this.CreatLineStyle(inputDoc);
		}

		private GraphicsStyle FindSuitStyle(Autodesk.Revit.DB.Document inputDoc)
		{
			List<Element> list = new FilteredElementCollector(inputDoc).OfClass(typeof(GraphicsStyle)).Where(this.CheckStyle()).ToList<Element>();
			GraphicsStyle result;
			if (list != null && list.Count != 0)
			{
				result = (list[0] as GraphicsStyle);
			}
			else
			{
				result = null;
			}
			return result;
		}

		private GraphicsStyle CreatLineStyle(Autodesk.Revit.DB.Document inputDoc)
		{
			Category category = inputDoc.Settings.Categories.get_Item(this.m_useLineCategory);
			Category category2 = inputDoc.Settings.Categories.NewSubcategory(category, this.m_strTempLineStyelName + Guid.NewGuid().ToString());
			category2.LineColor = this.m_lineStyelColor;
			category2.SetLineWeight(this.m_useLineWieight, this.m_useStyleType);
			return category2.GetGraphicsStyle(this.m_useStyleType);
		}

		private Func<Element, bool> CheckStyle()
		{
			return (Element e) => e != null && e is GraphicsStyle && e.Category != null && e.Category.LineColor.Red == this.m_lineStyelColor.Red && e.Category.LineColor.Green == this.m_lineStyelColor.Green && e.Category.LineColor.Blue == this.m_lineStyelColor.Blue && e.Category.GetLineWeight(this.m_useStyleType) != null && e.Category.GetLineWeight(this.m_useStyleType) == this.m_useLineWieight;
		}

		private List<Curve> m_lstAllCurves = new List<Curve>();

		private string m_strTempLineStyelName = "hyERLine";

		private string m_useLinePatternName = "Dash";

        private Autodesk.Revit.DB.Color m_lineStyelColor = new Autodesk.Revit.DB.Color(0, byte.MaxValue, 0);

		private int m_useLineWieight = 1;

        private GraphicsStyleType m_useStyleType = (GraphicsStyleType)1;

		private BuiltInCategory m_useLineCategory = (BuiltInCategory)(-2000051);

        private BuiltInParameter m_useLineStyelParameter = (BuiltInParameter)(-1006210);
	}
}
